﻿using Unity.Collections;
using UnityEngine;
using UnityEngine.ParticleSystemJobs;

namespace HitNumber.Demo
{
    /// <summary>
    /// make particle lookat this transform
    /// </summary>
    public class LookAtPointParticle : MonoBehaviour
    {
        [SerializeField]
        Vector3 Aixs;

        [Min(1)]
        [SerializeField]
        int NumPerCircle;
        private ParticleSystem _ps;
        private UpdateParticlesIconJob _job = new UpdateParticlesIconJob();
        private void Awake()
        {
            _ps = GetComponent<ParticleSystem>();
            var main = _ps.main;
            main.startRotation3D = enabled;
        }
        private void OnParticleUpdateJobScheduled()
        {
            _job.Schedule(_ps);
        }
        private void Update()
        {
            _job.Point = transform.position;
            _job.NumPerCircle = NumPerCircle;
        }
        struct UpdateParticlesIconJob : IJobParticleSystem
        {
            [ReadOnly]
            public Vector3 Point;
            [ReadOnly]
            public int NumPerCircle;
            public void Execute(ParticleSystemJobData particles)
            {
                var rotationX = particles.rotations.x;
                var rotationY = particles.rotations.y;
                var rotationZ = particles.rotations.z;
                var position = particles.positions;
                for (int i = 0; i < particles.count; i++)
                {
                    var dir = -position[i];
                    //var quaternion = Quaternion.LookRotation(dir);

                    ////quaternion *= Quaternion.Euler(Aixs.x, Aixs.y, Aixs.z);
                    //var eulerAngles = quaternion.eulerAngles;
                    //rotationX[i] = eulerAngles.x;
                    //rotationY[i] = eulerAngles.y;
                    //rotationZ[i] = eulerAngles.z;
                    rotationZ[i] = i * (360f / NumPerCircle);
                }
            }
        }
    }
}